package net.huashitong.interview.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.core.filter.BaseFilter;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.interview.entity.NeedSolveReadState;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ReadStateDao extends StringDao<NeedSolveReadState> {


    public boolean getReadState(String needSolveId,String userId){
        //根据列表Id跟用户Id,查询阅读状态
        GenericQuery query = createQuery("obj");
        query.append(" and obj.needSolveId =:needSolveId").setParam("needSolveId", needSolveId);
        query.append(" and obj.userId =:userId").setParam("userId", userId);
        List<NeedSolveReadState> list = query.listResult();

        if(ObjectUtils.isBlank(list)&&list.size()==0){
            return false;
        }else{
            if("true".equals(list.get(0).getReadState())){
                return true;
            }else{
                return false;
            }
        }
    }

    public NeedSolveReadState getBySolveIdAndUserId(String needSolveId,String userId){
        GenericQuery query = createQuery("obj");
        query.append(" and obj.needSolveId =:needSolveId").setParam("needSolveId", needSolveId);
        query.append(" and obj.userId =:userId").setParam("userId", userId);
        query.setOrder("sysId",BaseFilter.DESC);
        NeedSolveReadState result = query.firstResult();
        return result;
    }
}
